package com.iweb;

import java.util.Objects;
import java.util.TreeSet;

public class Demo4 {
    public static void main(String[] args) {
        TreeSet<Person> person = new TreeSet<>();
        person.add(new Person("小王",20,"男"));
        person.add(new Person("小明",22,"男"));

    }
}

class Person implements Comparable<Person>{
    private String name;
    private int age;
    private String gender;

    public Person(){

    }
    public Person(String name,int age,String gender){
        this.name=name;
        this.age=age;
        this.gender=gender;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                '}';
    }

    public int getAge() {
        return age;
    }

    @Override
    public int compareTo(Person o) {
        //自己定义一个规则？
        int num=this.age-o.getAge();
        return num;
    }
}
